package admin;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.common.Lunar;
import com.filter.BaseController;

import beans.sys.UserBean;
import service.SpringContextUtil;
import service.sys.FunServiceImpl;

@Controller
@RequestMapping("/home")
public class HomeController extends BaseController {

	@Resource
	private FunServiceImpl funServiceImpl;

	@RequestMapping("/AdminLTE")
	public String Index(){
		/*int l = SpringContextUtil.getApplicationContext().getBeanDefinitionNames().length;
		for(int i=0;i<l;i++){
			System.out.println(SpringContextUtil.getApplicationContext().getBeanDefinitionNames()[i]);
		}*/
		
		UserBean user = (UserBean)getReq().getSession().getAttribute("user");
		getReq().setAttribute("username", user.getUsername());

		return "index";
	}

	@RequestMapping("/main")
	public String Main(){
		UserBean user = (UserBean)getReq().getSession().getAttribute("user");
		Calendar cal=Calendar.getInstance();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		cal.setTimeZone(TimeZone.getDefault());
		Lunar lunar=new Lunar(cal);
		//sdf.format(cal.getTime())
		getReq().setAttribute("date", String.format("农历%s年%s月%s",lunar.getYear(),lunar.getMonth(),lunar.getChinaDayString(lunar.getDay())));

		return "main";
	}

	@ResponseBody
	@RequestMapping("/GetClientDataJson")
	public void GetClientDataJson(){
		UserBean user = (UserBean)getReq().getSession().getAttribute("user");
		List<Map> listMenu = funServiceImpl.getAll(user.getId());
		Map<String,List<Map>> ret = new HashMap<String, List<Map>>();
		List<Map> funTrees = new ArrayList<Map>();

		for(Object obj : listMenu){
			Map<String,String> row = (Map<String,String>) obj;
			row.put("ParentId", row.get("pid"));
			row.put("FullName", row.get("title"));
			row.put("Icon", row.get("iconCls"));
			row.put("ModuleId", row.get("id"));
			row.put("UrlAddress", row.get("linkurl"));
			row.put("IsMenu", "1");
			row.put("IsPublic", "0");
			row.put("Target", "iframe");
			funTrees.add(row);
		}
		ret.put("authorizeMenu", funTrees);
		
		responseJson(ret);
	}
}
